<?php

session_start();
ob_start();
include_once dirname(dirname(__FILE__)) . '/include/common.php';
$page = isset($_POST["page"]) ? intval($_POST["page"]) : 1;

$core = new core();
$load_more = 0;
$zen_order_id = isset($_POST["zen_order_id"]) ? intval($_POST["zen_order_id"]) : 0;
$by_product_customer = isset($_POST["by_product_customer"]) ? intval($_POST["by_product_customer"]) : 0;
$by_order_status = isset($_POST["by_order_status"]) ? intval($_POST["by_order_status"]) : 0;
$by_province = isset($_POST["by_province"]) ? intval($_POST["by_province"]) : 0;
$by_payment = isset($_POST["by_payment"]) ? intval($_POST["by_payment"]) : 0;
$search_text = isset($_POST["search_text"]) ? addslashes(trim($_POST["search_text"])) : '';
$category_id = isset($_POST["category_id"]) ? intval(trim($_POST["category_id"])) : 0;
$start_time = isset($_POST["start_time"]) ? addslashes(trim($_POST["start_time"])) : '';
$end_time = isset($_POST["end_time"]) ? addslashes(trim($_POST["end_time"])) : '';
$order_by = '';

if (!$core->checkZenOrderByID($zen_order_id)) {
    echo json_encode(array('type' => 1, 'msg' => "Đợt hàng này không tồn tại!"));
    die;
}

if ($by_product_customer == 0 && $by_order_status == 0 && $by_province == 0 && $by_payment == 0 && $search_text != "") {
    echo json_encode(array('type' => 1, 'msg' => "Vui lòng chọn 1 trong 4 sự lựa chọn để sắp xếp!"));
    die;
}
if ($start_time != "" && $end_time != "") {
    if (strtotime($end_time) <= strtotime($start_time)) {
        echo json_encode(array('type' => 1, 'msg' => "Bạn phải chọn thời gian kết thúc lớn hơn thời gian bắt đầu!"));
        die;
    }
}
if ($start_time != "") {
    $start_time = $core->changeDateMysqlToCompare(date("Y-m-d H:i:s", strtotime($start_time)));
}
if ($end_time != "") {
    $end_time = $core->changeDateMysqlToCompare(date("Y-m-d H:i:s", strtotime($end_time)));
}

$where = "";

if ($by_product_customer == 1) {
    $where = "SELECT * FROM zen_customer_order_detail
              WHERE customer_order_id IN (SELECT customer_order_id FROM zen_custome_order WHERE zen_order_id= {$zen_order_id} ) ";
    //tìm kiếm theo sản phẩm
    if ($by_order_status > 0) {
        //tìm sản phẩm theo trạng thái
        switch ($by_order_status) {
            case 1:
                //store
                $where.=' AND order_status=0';
                if ($start_time != "") {
                    $where.=' AND store_date >="' . $start_time . '"';
                }
                if ($end_time != "") {
                    $where.=' AND store_date<"' . $end_time . '"';
                }
                $order_by .= "store_date asc";
                break;
            case 2:
                //online
                $where.=' AND order_status=1';
                if ($start_time != "") {
                    $where.=' AND online_date >="' . $start_time . '"';
                }
                if ($end_time != "") {
                    $where.=' AND online_date<"' . $end_time . '"';
                }
                $order_by .= "online_date asc";
                break;
            case 3:
                //hàng có sẵn
                $where.=' AND order_status=2';
                break;
            case 4:
                //đã nhận hàng
                $where.=' AND order_status=3';
                if ($start_time != "") {
                    $where.=' AND received_date >="' . $start_time . '"';
                }
                if ($end_time != "") {
                    $where.=' AND received_date<"' . $end_time . '"';
                }
                $order_by .= "received_date asc";
                break;
            case 5:
                //đang shipping
                $where.=' AND order_status=4';
                break;
            case 6:
                //khách đã nhận
                $where.=' AND order_status=5';
                if ($start_time != "") {
                    $where.=' AND saled_date >="' . $start_time . '"';
                }
                if ($end_time != "") {
                    $where.=' AND saled_date<"' . $end_time . '"';
                }
                $order_by .= "saled_date asc";
                break;
            case 7:
                //hết hàng
                $where.=' AND order_status=6';
                break;
            case 8:
                //ORDER
                $where.=' AND order_status=7';
                if ($start_time != "") {
                    $where.=' AND customer_order_date >="' . $start_time . '"';
                }
                if ($end_time != "") {
                    $where.=' AND customer_order_date <"' . $end_time . '"';
                }
                $order_by .= "customer_order_date asc";
                break;
            case 9:
                //chưa chắc chắn
                $where.=' AND order_status=8';
                break;
        }
    }

    $where_product = '';
    //tìm sản phẩm theo category
    if($category_id > 0){
        $where_product.="  product_category={$category_id} ";
    }

    //tìm sản phẩm theo mã code
    if ($search_text !== "") {
        if($where_product==""){
            $where_product.=" product_code LIKE '%" . $search_text . "%'";
        }else{
            $where_product.=" AND product_code LIKE '%" . $search_text . "%'";
        }

    }

    if($where_product!=''){
        $where.=' AND product_id IN (SELECT product_id FROM zen_product WHERE '.$where_product.' )';
    }

} elseif ($by_product_customer == 2) {
    //tìm kiếm theo khách hàng
    $where = "SELECT * FROM zen_customer_order_detail
              WHERE customer_order_id IN (SELECT customer_order_id FROM zen_custome_order WHERE zen_order_id= {$zen_order_id} ) ";
    $order_by .= ", facebook_name asc";
}


if ($order_by != "") {
    //ORDER: sắp xếp ngày đặt mua tăng dần
    if ($by_product_customer == 1) {
        $order_by .= ", product_code asc";
    } elseif ($by_product_customer == 2) {
        $order_by .= ", facebook_name asc";
    }
} else {
    if ($by_product_customer == 1) {
        $order_by .= "product_code asc";
    } elseif ($by_product_customer == 2) {
        $order_by .= "facebook_name asc";
    }
}
if ($by_payment > 0) {
    switch ($by_payment) {
        case 1:
            //thanh toán hết
            if ($where == '') {
                $where.='total_current_money=0';
            } else {
                $where.=' AND total_current_money=0';
            }
            break;
        case 2:
            //thanh toán thừa
            if ($where == '') {
                $where.='total_current_money > 0';
            } else {
                $where.=' AND total_current_money > 0';
            }
            break;
        case 3:
            //thanh toán thiếu
            if ($where == '') {
                $where.='total_current_money < 0';
            } else {
                $where.=' AND total_current_money < 0';
            }
            break;
        case 4:
            //đã cọc
            if ($where == '') {
                $where.='total_payment > 0';
            } else {
                $where.=' AND total_payment > 0';
            }
            break;
        case 5:
            //chưa thanh toán
            if ($where == '') {
                $where.='total_payment = 0';
            } else {
                $where.=' AND total_payment = 0';
            }
            break;
    }
}

if ($search_text !== "") {
    if ($by_product_customer == 1) {
        if ($where == '') {
            $where.="product_code LIKE '%" . $search_text . "%'";
        } else {
            $where.=" AND product_code LIKE '%" . $search_text . "%'";
        }
    } elseif ($by_product_customer == 2) {
        if ($where == '') {
            $where.="facebook_name LIKE '%" . $search_text . "%'";
        } else {
            $where.=" AND facebook_name LIKE '%" . $search_text . "%'";
        }
    }
}

if ($by_province > 0) {
    switch ($by_province) {
        case 1:
            //tỉnh
            if ($where == '') {
                $where.='province=1';
            } else {
                $where.=' AND province=1';
            }
            break;
        case 2:
            //Hà Nội
            if ($where == '') {
                $where.='province=0';
            } else {
                $where.=' AND province=0';
            }
            break;
    }
}

$last_customer_order_id = isset($_POST["last_customer_order_id"]) ? intval($_POST["last_customer_order_id"]) : 0;
$var_count = isset($_POST["var_count"]) ? intval($_POST["var_count"]) : 0;
if ($core->checkZenOrderByID($zen_order_id)) {

    $total_result = array();
    if ($by_order_status > 0 || $by_product_customer == 1) {
        $is_group_customer = 0;
    } else {
        $is_group_customer = 1;
    }
    $list_customer_order = $core->getListCustomerOrderFilterByCustomer($zen_order_id, $order_by, $where, $page, $load_more, $total_result, 20);
    $customer_order_id = 0;
    $html_total_result = '';
    if ($_SESSION["user_role"] != 3) {
        include_once '../templates/ajax/full_permission_order_filter.php';
    } else {
        include_once '../templates/ajax/view_permission_order_filter.php';
    }

    $page = $page + 1;
    echo json_encode(array('type' => 0, 'html' => $html, 'page' => $page, 'load_more' => $load_more, 'last_customer_order_id' => $customer_order_id, 'html_total_result' => $html_total_result, 'var_count' => $var_count));
    die;
}
?>